package com.it.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.commonutils.JwtUtils;
import com.it.commonutils.R;

import com.it.eduservice.client.OrdersClient;
import com.it.eduservice.entity.EduCourse;
import com.it.eduservice.entity.chapter.ChapterVo;
import com.it.eduservice.entity.frontvo.CourseFrontVo;
import com.it.eduservice.entity.frontvo.CourseWebVo;
import com.it.commonutils.ordervo.CourseWebVoOrder;
import com.it.eduservice.service.EduChapterService;
import com.it.eduservice.service.EduCourseService;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/courseFront")
//@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrdersClient ordersClient;

    //条件查询带分页课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody (required = false)CourseFrontVo courseFrontVo){
        Page<EduCourse>pageCourse=new Page<EduCourse>(page,limit);
        Map<String,Object>map=eduCourseService.getCourseFrontList(pageCourse,courseFrontVo);
        return R.ok().data(map);
    }

    //课程详情方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //基本信息
        CourseWebVo courseWebVo=eduCourseService.getBaseCourseInfo(courseId);
        //章节小节
        List<ChapterVo> chapterVideoList = eduChapterService.getChapterVideoByCourseId(courseId);

        //根据课程id和用户id查询课程支付状态

        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }

    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder=new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
